package com.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * util
 *
 * @author: wsl
 * @date: 2023/12/27 0027 22:08
 * @ClassName: MybatisPlusUtil
 */
public class MybatisPlusUtil {
    private static final Logger logger = LoggerFactory.getLogger(MybatisPlusUtil.class);
    public static String getSqlStr(LambdaQueryWrapper lambdaQueryWrapper){
        try {
            String customSqlSegment = lambdaQueryWrapper.getCustomSqlSegment();
            if (StringUtils.isBlank(customSqlSegment)){
                return "";
            }
            Map<String, Object> paramNameValuePairs = lambdaQueryWrapper.getParamNameValuePairs();
            for (int i = 1; i <= paramNameValuePairs.size(); i++) {
                String key = "MPGENVAL"+i;
                if (paramNameValuePairs.containsKey(key)){
                    customSqlSegment = customSqlSegment.replace("#{ew.paramNameValuePairs.MPGENVAL"+i+"}","\""+String.valueOf(paramNameValuePairs.get(key))+"\"");
                }

            }
            return customSqlSegment;
        }catch (Exception e){
            logger.error("解析sql语句异常",e);
        }
        return "";
    }
    private static String parseSql(String objectSql,Map<String,Object> paramsMap){
        if (StringUtils.isBlank(objectSql)){
            return "";
        }
        for (int i = 1; i <= paramsMap.size(); i++) {
            String key = "MPGENVAL"+i;
            if (paramsMap.containsKey(key)){
                objectSql = objectSql.replace("#{ew.paramNameValuePairs.MPGENVAL"+i+"}","\""+String.valueOf(paramsMap.get(key))+"\"");
            }

        }
        return objectSql;
    }

    public static void main(String[] args) {
        String str = "WHERE (temp_id IN (#{ew.paramNameValuePairs.MPGENVAL1},#{ew.paramNameValuePairs.MPGENVAL2},#{ew.paramNameValuePairs.MPGENVAL3},#{ew.paramNameValuePairs.MPGENVAL4}) AND (suppliers_id IN (#{ew.paramNameValuePairs.MPGENVAL5},#{ew.paramNameValuePairs.MPGENVAL6},#{ew.paramNameValuePairs.MPGENVAL7},#{ew.paramNameValuePairs.MPGENVAL8},#{ew.paramNameValuePairs.MPGENVAL9},#{ew.paramNameValuePairs.MPGENVAL10},#{ew.paramNameValuePairs.MPGENVAL11},#{ew.paramNameValuePairs.MPGENVAL12},#{ew.paramNameValuePairs.MPGENVAL13},#{ew.paramNameValuePairs.MPGENVAL14},#{ew.paramNameValuePairs.MPGENVAL15}) AND (is_europe <> #{ew.paramNameValuePairs.MPGENVAL16} OR (is_europe = #{ew.paramNameValuePairs.MPGENVAL17} AND mer_id <> #{ew.paramNameValuePairs.MPGENVAL18} AND convert_is_europe IS NOT NULL))) AND (keyword LIKE #{ew.paramNameValuePairs.MPGENVAL19} OR store_name LIKE #{ew.paramNameValuePairs.MPGENVAL20}) AND is_show = #{ew.paramNameValuePairs.MPGENVAL21} AND is_del = #{ew.paramNameValuePairs.MPGENVAL22} AND id NOT IN (SELECT product_id FROM eb_store_product_mer_30_mp WHERE is_not_show = 1) AND stock > #{ew.paramNameValuePairs.MPGENVAL23} AND cate_id NOT IN (#{ew.paramNameValuePairs.MPGENVAL24},#{ew.paramNameValuePairs.MPGENVAL25},#{ew.paramNameValuePairs.MPGENVAL26},#{ew.paramNameValuePairs.MPGENVAL27})) ORDER BY last_push_time DESC";

        Map<String, Object> stringObjectMap = JSON.parseObject("{\"MPGENVAL3\":\"3\",\"MPGENVAL2\":\"2\",\"MPGENVAL1\":\"1\",\"MPGENVAL27\":4789,\"MPGENVAL26\":4788,\"MPGENVAL25\":4787,\"MPGENVAL24\":4786,\"MPGENVAL23\":0,\"MPGENVAL22\":false,\"MPGENVAL21\":true,\"MPGENVAL20\":\"%4250947544686%\",\"MPGENVAL19\":\"%4250947544686%\",\"MPGENVAL18\":30,\"MPGENVAL17\":\"INSIDE\",\"MPGENVAL16\":\"INSIDE\",\"MPGENVAL15\":\"203\",\"MPGENVAL14\":\"243\",\"MPGENVAL13\":\"197\",\"MPGENVAL9\":\"181\",\"MPGENVAL12\":\"199\",\"MPGENVAL8\":\"180\",\"MPGENVAL11\":\"191\",\"MPGENVAL7\":\"176\",\"MPGENVAL10\":\"186\",\"MPGENVAL6\":\"175\",\"MPGENVAL5\":\"174\",\"MPGENVAL4\":\"11\"}", new TypeReference<Map<String, Object>>() {

        });
        System.out.println(parseSql(str, stringObjectMap));

    }
}
